using System.ComponentModel;
using HelpDeskTool.Entities;
using HelpDeskTool.Extensions;

namespace HelpDeskTool.Views;

public class ComputerView : IComputerView, INotifyPropertyChanged, IEquatable<IComputer>
{
	private readonly IComputer computer;

	public event PropertyChangedEventHandler? PropertyChanged;

	public ComputerView(IComputer computer)
	{
		this.computer = computer;
	}

	protected void Notify(string property) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));

	public string Name => computer.Name;
	public string? Description => computer.Description;
	public string? Phone => computer.Phone;
	public string? IPaddr => computer.Information?.IpAddresses?.ToStringWithDelimiter();
	public WmiInfo? Hardware => computer.Information?.Hardware != null ? computer.Information : null;
	public bool Offline => (computer.Information?.Updated?.AddSeconds(100) > DateTime.Now) == false;
	public DateTime? LastBootUp => computer.Information?.LastBootUp;

	public DetailInfo? Information
	{
		get => computer.Information;
		set
		{
			computer.Information = value;

			Notify(nameof(Hardware));
			Notify(nameof(IPaddr));
			Notify(nameof(Offline));
			Notify(nameof(Information));
			Notify(nameof(LastBootUp));
		}
	}

	public override string ToString() => $"{Name} {Description} {Phone} {IPaddr}";

	public bool Equals(IComputer? other)
	{
		if (other == null)
			return false;

		if (other == this)
			return true;

		return Name.Equals(other.Name, StringComparison.OrdinalIgnoreCase);
	}

	public override bool Equals(object? obj) => Equals(obj as IComputer);

	public override int GetHashCode() => Name.GetHashCode();
}
